package com.ruoyi.dm.component;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.component.manager.TrackDataManager;
import com.ruoyi.dm.domain.BO.TrackDataBO;
import com.ruoyi.dm.domain.po.SpaceTrackCollectRecordPO;
import com.ruoyi.trackapply.domain.CollectGnssPO;
import common.StOrbitPoint;
import lombok.extern.slf4j.Slf4j;
import org.orekit.data.DataContext;
import org.orekit.data.DataProvidersManager;
import org.orekit.data.DirectoryCrawler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import plugins.ImportFile;

import java.io.File;
import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


/**
 * 空间目标数据 精密轨道
 */
@Component
@Slf4j
public class TrackGracefoStrategy implements DataStrategy<SpaceTrackCollectRecordPO> {


    @Autowired
    private TrackDataManager trackDataManager;

    @Override
    public SpaceTrackCollectRecordPO makeupPo(SpaceTrackCollectRecordPO po) {
        //GNI1B_2022-01-01_C_04.txt
        String fileName = po.getFileName();
        String[] split = fileName.split("_");
        //po.setDataType(split[1]);
        DateTime date = DateUtil.parse(split[1], "yyyy-MM-dd");
        po.setFileDate(date);
        return po;
    }

    @Override
    public String pythonCmd(String satellite, String dataType, String filePath, String method, Integer year) {

        return null;
    }



    @Override
    public void save(String satellite, String outFile) throws Exception {

        //需先加载星历数据
        String dataPath = PathUtil.configDir() + "/collect/orekit";
        File orekitData = new File(dataPath);
        DataProvidersManager manager = DataContext.getDefault().getDataProvidersManager();
        manager.addProvider(new DirectoryCrawler(orekitData));

        //输入数据,卫星轨道数据
        List<StOrbitPoint> satOrbitDataLst= new ArrayList<>();
        ImportFile.readGracefoOrbitData(outFile,satOrbitDataLst);
        log.info("解析:{} size {}",outFile,satOrbitDataLst.size());
        List<TrackDataBO> list = new ArrayList<>();
        for (StOrbitPoint point : satOrbitDataLst) {
            TrackDataBO build = TrackDataBO.builder().position_x(point.getDx()).position_y(point.getDy()).position_z(point.getDz())
                    .speed_vx(point.getDvx()).speed_vy(point.getDvy()).speed_vz(point.getDvz()).obs_time(DateUtil.parse(point.getStrEpoch(),"yyyy-MM-dd HH:mm:ss")).build();
            list.add(build);
        }
        satOrbitDataLst = null;
        //数据入库
        trackDataManager.saveData(satellite,list);
    }

    public static void main(String[] args) {
        TrackGracefoStrategy trackGracefoStrategy = new TrackGracefoStrategy();

        File orekitData = new File("D:\\jieya\\orekit-data");
        DataProvidersManager manager = DataContext.getDefault().getDataProvidersManager();
        manager.addProvider(new DirectoryCrawler(orekitData));
        String outFile = "D:\\jieya\\gracefoc\\轨道\\GNI1B_2022-01-01_C_04.txt";
        String file = "D:\\jieya\\gracefoc\\轨道\\真值.txt";
        List<StOrbitPoint> satOrbitDataLst= new ArrayList<>();
        ImportFile.readGracefoOrbitData(outFile,satOrbitDataLst);
        log.info("解析:{} size {}",outFile,satOrbitDataLst.size());
        List<CollectGnssPO> list = new ArrayList<>();
        for (StOrbitPoint point : satOrbitDataLst) {
            //2021-12-31T23:59:42.000
            TrackDataBO build = TrackDataBO.builder().position_x(point.getDx()).position_y(point.getDy()).position_z(point.getDz())
                    .speed_vx(point.getDvx()).speed_vy(point.getDvy()).speed_vz(point.getDvz()).obs_time(DateUtil.parse(point.getStrEpoch(), DateTimeFormatter.ISO_LOCAL_DATE_TIME)).build();
            CollectGnssPO collectGnssPO = new CollectGnssPO();
            collectGnssPO.setObs_time(DateUtil.parse(point.getStrEpoch(),DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            collectGnssPO.setPosition_x(new BigDecimal(point.getDx()));
            collectGnssPO.setPosition_y(new BigDecimal(point.getDy()));
            collectGnssPO.setPosition_z(new BigDecimal(point.getDz()));
            collectGnssPO.setSpeed_vx(new BigDecimal(point.getDvx()));
            collectGnssPO.setSpeed_vy(new BigDecimal(point.getDvy()));
            collectGnssPO.setSpeed_vz(new BigDecimal(point.getDvz()));
            list.add(collectGnssPO);
        }
        FileUtil.writeLines(list, file, "utf-8");
    }

}
